﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Model.CSPEnum;
using CSP.Model.CSP;
using CSP.Bll;

namespace CSP.Web.UserControls
{
    public partial class HomeRecommend : System.Web.UI.UserControl
    {
        private string _serviceIds = "";

        private HomeRecommendType _currentHomeRecommendType = HomeRecommendType.HomeTopRecommend;


        /// <summary>
        /// 推荐类型
        /// </summary>
        public HomeRecommendType CurrentHomeRecommendType
        {
            get { return _currentHomeRecommendType; }

            set { _currentHomeRecommendType = value; }
        }


        private RecommendType _recommendType;

        private int _count = 0;


        /// <summary>
        /// 推荐类型
        /// </summary>
        public RecommendType RecommendType
        {
            get
            {
                return _recommendType;
            }
            set
            {
                _recommendType = value;
            }
        }

        /// <summary>
        /// 记录数量
        /// </summary>
        public int RecordCount
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
            }
        }

        /// <summary>
        /// 需要展现的服务Ids
        /// </summary>
        public string ServiceIds
        {
            get
            {
                return _serviceIds;
            }
            set
            {
                _serviceIds = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindServiceInfoUI();
            }
        }

        /// <summary>
        /// 绑定服务信息UI
        /// </summary>
        private void BindServiceInfoUI()
        {
            //List<int> serviceIds = _serviceIds.Split(',').ToList().ConvertAll<int>(new Converter<string, int>(m => Convert.ToInt32(m)));
            SearchServiceList[] searchServiceLists = GetDataByIds(_recommendType, _count);
            this.R_ServiceInfo.DataSource = searchServiceLists;
            this.R_ServiceInfo.DataBind();
        }


        /// <summary>
        /// 获取推荐数据
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public SearchServiceList[] GetDataByIds(RecommendType recommendType, int count)
        {
            SearchServiceList[] result;
            using (var recommendBll = CSPBllFactory.GetInstance().GetRecommendBll())
            {
                result = recommendBll.GetSearchServiceListByRecommendTypeAndCount(recommendType, count);
            }
            return result;
        }

        /// <summary>
        /// 根据认证类型和认证状态获取 当前认证名称
        /// </summary>
        /// <param name="personalAuthentication"></param>
        /// <param name="enterpriseAuthentication"></param>
        /// <returns></returns>
        protected string GetCNNameByAuthentication(int personalAuthentication, int enterpriseAuthentication)
        {
            if ((AuthenticationStatus)enterpriseAuthentication == AuthenticationStatus.Accomplish)
            {
                return "企业认证";
            }

            if ((AuthenticationStatus)personalAuthentication == AuthenticationStatus.Accomplish)
            {
                return "个人认证";
            }

            return "未认证";
        }

        /// <summary>
        /// 获取样式根据企业认证和个人认证 状态
        /// </summary>
        /// <param name="personalAuthentication"></param>
        /// <param name="enterpriseAuthentication"></param>
        /// <returns></returns>
        protected string GetStyleByAuthentication(int personalAuthentication, int enterpriseAuthentication)
        {
            //企业认证
            if ((AuthenticationStatus)enterpriseAuthentication == AuthenticationStatus.Accomplish)
            {
                return "i-v-enterprise";
            }

            //个人认证
            else if ((AuthenticationStatus)personalAuthentication == AuthenticationStatus.Accomplish)
            {
                return "i-v-personal";
            }
            else
            {
                return "i-v-unautherized";
            }
        }

        /// <summary>
        /// 根据价格类型，现价格，最低价格，最高价格，获取价格
        /// </summary>
        /// <param name="priceType"></param>
        /// <param name="price"></param>
        /// <param name="priceStart"></param>
        /// <param name="priceEnd"></param>
        /// <returns></returns>
        protected string GetPriceByPriceTypeAndPriceAndPriceStartAndPriceEnd(int priceType, decimal price, decimal priceStart, decimal priceEnd)
        {
            string result = "";
            if ((PriceType)priceType == PriceType.FixedPrice) //一口价
            {
                result = price.ToString("0.00");
            }
            else if ((PriceType)priceType == PriceType.PriceRange)//价格范围
            {
                result = priceStart.ToString("0.00") + "--" + priceEnd.ToString("0.00");
            }
            else if ((PriceType)priceType == PriceType.Negotiable)//面议
            {
                result = "面议";
            }
            return result;
        }

        /// <summary>
        /// 根据当前价格判断是否显示折扣
        /// </summary>
        /// <param name="price"></param>
        /// <param name="originalPrice"></param>
        /// <returns></returns>
        protected bool IsRebateByCurrentPrice(decimal price, decimal originalPrice)
        {
            if (price == 0 || originalPrice == 0)
            {
                return false;
            }
            else if (price / originalPrice < 1)
            {
                return true;
            }
            return false;

        }

        /// <summary>
        /// 是否显示折扣Icon
        /// </summary>
        /// <returns></returns>
        protected bool IsShowRebateIcon()
        {
            if (_currentHomeRecommendType == HomeRecommendType.HomeTopRecommend)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public enum HomeRecommendType
        {
            /// <summary>
            /// 头部推荐
            /// </summary>
            HomeTopRecommend = 0,

            /// <summary>
            /// 分类推荐
            /// </summary>
            HomeCategoryRecommend = 1,
        }
    }
}